// $Id: Export.cs 71 2008-06-24 20:21:42Z nvivo $

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace DBLGen.GUI.Forms
{
	public partial class Export : Form
	{
		private Project _project;

		public Export(Project project)
		{
			if (project == null)
				throw new ArgumentNullException("project");

			InitializeComponent();

			_project = project;
		}

		private void Export_Load(object sender, EventArgs e)
		{
			txtFolder.Text = Path.Combine(Environment.CurrentDirectory, "Code");

			txtFolder.Text = @"D:\Dev\SrvZh02\Quizbutler\Frontstore\";

			EncodingInfo[] encodings = Encoding.GetEncodings();
			Array.Sort(encodings, EncodingInfoComparer);

			cmbEncoding.DataSource = encodings;

			cmbEncoding.SelectedIndex = cmbEncoding.FindString(Encoding.UTF8.EncodingName);
		}

		private void cmbEncoding_Format(object sender, ListControlConvertEventArgs e)
		{
			EncodingInfo ei = (EncodingInfo)e.ListItem;
			e.Value = String.Format("{0} [{1}]", ei.DisplayName, ei.Name);
		}

		private static int EncodingInfoComparer(EncodingInfo a, EncodingInfo b)
		{
			return String.Compare(a.DisplayName, b.DisplayName);
		}

		private void btnBrowse_Click(object sender, EventArgs e)
		{
			using (FolderBrowserDialog dlg = new FolderBrowserDialog())
			{
				dlg.SelectedPath = txtFolder.Text;

				if (dlg.ShowDialog() == DialogResult.OK)
				{
					txtFolder.Text = dlg.SelectedPath;
				}
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			EncodingInfo ei = (EncodingInfo)cmbEncoding.SelectedItem;
			Encoding encoding = ei.GetEncoding();

			string targetFolder = txtFolder.Text;

			PluginInfo pi = _project.ExportPlugin;
			ExportPlugin ep = (ExportPlugin)pi.CreatePlugin();

			int tableCount = 0;
			int fileCount = 0;

			foreach (Table t in _project.Tables)
			{
				if (t.CanExport)
				{
					PluginSettings settings = pi.CreateSettings();

					if (!settings.LoadFrom(_project.SettingsManager, t.UniqueID.ToString()))
						settings.LoadFrom(_project.SettingsManager);

					ep.Settings = settings;

					ExportDataCollection edc = ep.Export(t);

					foreach (ExportData ed in edc)
					{
						string targetFilePath = Path.Combine(targetFolder, ed.FileName);
						string folder = Path.GetDirectoryName(targetFilePath);

						if (!Directory.Exists(folder))
							Directory.CreateDirectory(folder);

						File.WriteAllText(targetFilePath, ed.Contents, encoding);

						fileCount++;
					}

					tableCount++;
				}
			}

			MessageBox.Show(String.Format("Done! {0} tables exported, {1} files generated.", tableCount, fileCount));
			Close();
		}

		private void txtFolder_Leave(object sender, EventArgs e)
		{
			try
			{
				txtFolder.Text = Path.GetFullPath(txtFolder.Text);
			}
			catch
			{
				txtFolder.Undo();
			}
		}
	}
}
